from tortoise import fields
from tortoise.models import Model

class RequestLog(Model):
    id = fields.IntField(pk=True, description="自增主键")
    request_time = fields.DatetimeField(auto_now_add=True, description="请求时间")
    ip_address = fields.CharField(max_length=64, description="IP地址")
    user_agent = fields.CharField(max_length=256, description="浏览器标识")
    browser = fields.CharField(max_length=32, null=True, description="浏览器类型")
    path = fields.CharField(max_length=256, description="请求路径")
    method = fields.CharField(max_length=16, description="请求方法")
    status_code = fields.IntField(description="响应状态码")
    params = fields.JSONField(null=True, description="请求参数")

    class Meta:
        table = "request_log"
        table_description = "通用请求日志表"
        ordering = ["-request_time"]


class LoginHistory(Model):
    id = fields.IntField(pk=True, description="自增主键")
    user = fields.ForeignKeyField("models.User", related_name="login_history", on_delete=fields.CASCADE, description="关联用户")
    login_time = fields.DatetimeField(auto_now_add=True, description="登录时间")
    ip_address = fields.CharField(max_length=64, description="IP地址")
    user_agent = fields.CharField(max_length=256, description="浏览器标识")
    browser = fields.CharField(max_length=32, null=True, description="浏览器类型")
    device = fields.CharField(max_length=128, null=True, description="设备信息")
    os = fields.CharField(max_length=64, null=True, description="操作系统")
    location = fields.CharField(max_length=128, null=True, description="登录地点")
    status = fields.CharField(max_length=32, description="登录状态", default="成功")

    class Meta:
        table = "login_history"
        table_description = "用户登录历史表"
        ordering = ["-login_time"] 
